$(document).ready(function() {
	verificationAndQuery();
	initButtons();
	newGoodsPage();
});

/**
 * 验证用户帐号并查询用户信息
 */
function verificationAndQuery() {

	var login  = sessionStorage["OESloginStat"];
	if(login != undefined  &&  login == "succeed"){
		var loaded = localStorage["OESLoaded"];
		if(loaded != undefined  &&  loaded == "true") {
			loadUserInfo();
			return;
		}
	}
	
	var logstate = localStorage["OESloginStat"];
	var uname    = localStorage["OESloginName"];
	var upass    = localStorage["OESloginPass"]; 
    
	if(logstate != undefined  &&  (logstate == "succeed"  ||  logstate == "auto") &&
		uname != null && uname != ""  &&  upass != null && upass != ""
	) {
		var autoLogin = (logstate != undefined  &&  logstate == "auto");

		userLogin(uname, upass, autoLogin, function () {
			loadUserInfo();
		}, function (failCode) {
			cleanUI();
			logout(true);
		});

	} else {cleanUI(); logout(false);}
}

function loadUserInfo() {
	//All is String

	var uid     = localStorage["OESuserid"];
	var uname   = localStorage["OESloginName"];
	var unick   = localStorage["OESusernick"];
	var ubank   = localStorage["OESuserbank"];
	var uscore  = localStorage["OESuserscore"];
	var utel    = localStorage["OESusertel"];
	var uvip    = localStorage["OESuservip"];
	var upic    = localStorage["OESuserpic"];
	var uqq     = localStorage["OESuserqq"];
	var uwechat = localStorage["OESuserwechat"];

	$("#hidUid").val(uid);
	$("#divMyHeadNick").text(unick);
	$("#divMyHeadId").text("id: " + uname);
	$("#aBank").text(ubank + "￥");
	$("#tdScore").text(uscore);
	$("#aVIP").text(uvip);
	$("#tdTel").text(utel);
	$("#tdQQ").text(uqq);
	$("#tdWechat").text(uwechat);

	$("#btnLogin").css("display", "none");
	$("#btnLogout").css("display", "block");
}

function cleanUI() {

	$("#hidUid").val("");
	$("#divMyHeadNick").text("未登录");
	$("#divMyHeadId").text("注册登录，成为靓仔！");
	$("#aBank").text("0.00￥");
	$("#tdScore").text("0");
	$("#aVIP").text("/");
	$("#tdTel").text("无");
	$("#tdQQ").text("无");
	$("#tdWechat").text("无");
    $("#aAddressCount").text("(0)");

	$("#btnLogout").css("display", "none");
	$("#btnLogin").css("display", "block");
}

function logoutBack() {
	if(confirm("真的要离开我吗T^T")) {
		cleanUI();
		logout(false);
	}
}

function toLogin() {
	cleanUI();
	logout(true);
}

function initButtons(){

	$("#btnLogout").click(logoutBack);
	$("#btnLogin").click(toLogin);

	$("#btnSearch").click(function() {

	});

	$("#btnNew").click(function() {
		newGoodsPage();
	});

	$("#btnType").click(function() {

	});

	$("#btnActive").click(function() {

	});

	$("#btnBonus").click(function() {

	});

	$("#btnHeadImg").click(function() {

	});

	$("#aBank").click(function() {

	});

	$("#aVIP").click(function() {

	});

	$("#btnManageAddress").click(function() {

	});

	$("#btnMyShopingSet").click(function() {

	});

	$("#btnMyOrder").click(function() {

	});

	$("#btnMyFavoGoods").click(function() {

	});

	$("#btnMyFavoStores").click(function() {

	});

	$("#btnMyMessage").click(function() {

	});

	$("#btnMyBeBoss").click(function() {
		myStorePage();
	});

	$("#imgMainLogo").click(function() {
		newGoodsPage();
	});
}

function lockWindowSize() {
	var minWidth0 = document.getElementById("divMainContent").clientWidth;
	var minWidth1 = document.getElementById("divMainBg").clientWidth;
	$("#divMainContent").css("min-width", minWidth0 + "px");
	$("#divMainContent").css("max-width", minWidth0 + "px");
	$("#divMainBg").css("min-width", minWidth1 + "px");
	$("#divMainBg").css("max-width", minWidth1 + "px");
}

function newGoodsPage() {
	lockWindowSize();
	document.title = "靓仔购物网 - 新品上新";
	document.getElementById("contentFrame").src = "mainTypeContent.jsp";
}

function myStorePage() {
	lockWindowSize();
	document.title = "靓仔购物网 - 我的店铺";
	document.getElementById("contentFrame").src = "mystore.jsp";
}